package com.sky.controller.admin;


import com.sky.result.Result;
import com.sky.utils.QnOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@Slf4j
@Api(tags = "公共服务相关接口")
@RestController
@RequestMapping("/admin/common")
public class CommonController {
    @Autowired
    QnOssUtil qnOssUtil;
    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file){
        //获取文件类型,文件名后缀,如.png
        String fileName = file.getOriginalFilename();
        fileName = fileName.substring(fileName.lastIndexOf("."));

        //生成随机文件名
        fileName = UUID.randomUUID().toString()+fileName;

        //这里额外指定了上传到服务器的哪个目录
        fileName = "img/" + fileName;

        //上传文件
        String path = null;
        try {
            byte[] bytes = file.getBytes();
            path = qnOssUtil.bytesUpload(bytes, fileName);

        } catch (IOException e) {
            e.printStackTrace();
        }

        return Result.success(path);
    }
}

